package com.mxpio.erp.plan.service.impl;

import com.mxpio.erp.common.plan.entity.OrganizationDemandLine;
import com.mxpio.erp.common.plan.entity.SalePredictionLine;
import com.mxpio.erp.common.sales.entity.SalesOrderLine;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpioframework.jpa.JpaUtil;
import org.springframework.stereotype.Service;

import com.mxpio.erp.common.plan.entity.MpsLine;
import com.mxpio.erp.common.plan.service.MpsLineService;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class MpsLineServiceImpl extends BaseServiceImpl<MpsLine> implements MpsLineService {
    @Override
    @Transactional
    public void associatedTransientFields(List<MpsLine> mpsLineList){
        if (mpsLineList!=null&&mpsLineList.size()>0){
            Map<String, List<MpsLine>> dataTypeMap = mpsLineList.stream().collect(Collectors.groupingBy(MpsLine::getDataType));
            Set<String> strings = dataTypeMap.keySet();
            for (String type:strings){
                List<MpsLine> subMpsLineList = dataTypeMap.get(type);
                if ("01".equals(type)){//销售订单
                    List<String> bizNos = subMpsLineList.stream().map(MpsLine::getBizNo).collect(Collectors.toList());
                    List<SalesOrderLine> soLines = JpaUtil.linq(SalesOrderLine.class).in("bizNo", bizNos).list();
                    Map<String, List<SalesOrderLine>> orderMap = soLines.stream().collect(Collectors.groupingBy(SalesOrderLine::getBizNo));
                    for (MpsLine line:subMpsLineList){
                        List<SalesOrderLine> salesOrderLines = orderMap.get(line.getBizNo());
                        Map<Object, SalesOrderLine> salesOrderLineMap = JpaUtil.index(salesOrderLines,"lineNo");
                        SalesOrderLine salesOrderLine = salesOrderLineMap.get(line.getBizLineNo());
                        if (salesOrderLine!=null){
                            line.setItemName(salesOrderLine.getItemName());
                            line.setItemSpec(salesOrderLine.getItemSpec());
                            line.setUnitCode(salesOrderLine.getUnitCode());
                            line.setDrawingNo(salesOrderLine.getDrawingNo());
                            line.setNeedQuantity(salesOrderLine.getQuantity());
                            line.setNeedDate(salesOrderLine.getDeliverDate());
                        }
                    }
                }else if ("02".equals(type)){//组织需求
                    List<String> bizNos = subMpsLineList.stream().map(MpsLine::getBizNo).collect(Collectors.toList());
                    List<OrganizationDemandLine> odLines = JpaUtil.linq(OrganizationDemandLine.class).in("bizNo", bizNos).list();
                    Map<String, List<OrganizationDemandLine>> oDMap = odLines.stream().collect(Collectors.groupingBy(OrganizationDemandLine::getBizNo));
                    for (MpsLine line:subMpsLineList){
                        List<OrganizationDemandLine> organizationDemandLines = oDMap.get(line.getBizNo());
                        Map<Object, OrganizationDemandLine> objectOrganizationDemandLineMap = JpaUtil.index(organizationDemandLines,"lineNo");
                        OrganizationDemandLine organizationDemandLine = objectOrganizationDemandLineMap.get(line.getBizLineNo());
                        if (organizationDemandLine!=null){
                            line.setItemName(organizationDemandLine.getItemName());
                            line.setItemSpec(organizationDemandLine.getItemSpec());
                            line.setUnitCode(organizationDemandLine.getUnitCode());
                            line.setDrawingNo(organizationDemandLine.getDrawingNo());
                            line.setNeedQuantity(organizationDemandLine.getQuantity());
                            line.setNeedDate(organizationDemandLine.getDemandDate());
                        }
                    }
                }else if ("03".equals(type)){//预留需求
                    List<String> itemCodes = subMpsLineList.stream().map(MpsLine::getItemCode).collect(Collectors.toList());
                    List<Item> dbItems = JpaUtil.linq(Item.class).in("itemCode", itemCodes).list();
                    Map<String, Item> dbItemsMap = JpaUtil.index(dbItems, "itemCode");
                    subMpsLineList.forEach(line->{
                        Item item = dbItemsMap.get(line.getItemCode());
                        if (item!=null){
                            line.setItemName(item.getItemName());
                            line.setItemSpec(item.getItemSpec());
                            line.setUnitCode(item.getUnitCode());
                            line.setDrawingNo(item.getDrawingNo());
                        }
                    });
                }else if ("04".equals(type)){//销售预测
                    List<String> bizNos = subMpsLineList.stream().map(MpsLine::getBizNo).collect(Collectors.toList());
                    List<SalePredictionLine> odLines = JpaUtil.linq(SalePredictionLine.class).in("bizNo", bizNos).list();
                    Map<String, List<SalePredictionLine>> oDMap = odLines.stream().collect(Collectors.groupingBy(SalePredictionLine::getBizNo));
                    for (MpsLine line:subMpsLineList){
                        List<SalePredictionLine> organizationDemandLines = oDMap.get(line.getBizNo());
                        Map<Object, SalePredictionLine> objectOrganizationDemandLineMap = JpaUtil.index(organizationDemandLines,"lineNo");
                        SalePredictionLine organizationDemandLine = objectOrganizationDemandLineMap.get(line.getBizLineNo());
                        if (organizationDemandLine!=null){
                            line.setItemName(organizationDemandLine.getItemName());
                            line.setItemSpec(organizationDemandLine.getItemSpec());
                            line.setUnitCode(organizationDemandLine.getUnitCode());
                            line.setDrawingNo(organizationDemandLine.getDrawingNo());
                            line.setNeedQuantity(organizationDemandLine.getQuantity());
                            line.setNeedDate(organizationDemandLine.getDemandDate());
                        }
                    }
                }
            }

        }
    }
}
